/*
 * @Author: xiaosihan 
 * @Date: 2021-04-30 22:13:23 
 * @Last Modified by: xiaosihan
 * @Last Modified time: 2021-11-07 14:29:51
 */

import React from "react";
import * as THREE from "three";
import Mesh from "../Mesh/Mesh";
import Object3D from "../Object3D/Object3D";
import Object3DComponent, { Object3DComponentProps } from "../Object3D/Object3DComponent";
import Transition from "../xrt_util/Transition";
import TransitionColor from "../xrt_util/TransitionColor";


export interface PointLightProps extends Object3DComponentProps {

    color?: string, // 光照颜色
    intensity?: number, // 光照强度
    castShadow?: boolean, // 创建阴影
    bias?: number, // 阴影贴图位移调整 避免出现精度问题
    decay?: number, //光照最近距离
    distance?: number // 光照最远距离
}

interface State {
}

const ball = new THREE.SphereBufferGeometry(0.5, 10, 10);

export default class PointLight extends Object3DComponent<PointLightProps, State> {
    constructor(props: PointLightProps) {
        super(props);

        this.object3d = new THREE.PointLight() as THREE.PointLight & Object3D;
    }

    static defaultProps = {
        color: "#ffffff", // 光照颜色
        intensity: 1, // 光照强度
        castShadow: true, // 创建阴影
        decay: 0, //光照最近距离
        distance: 100, // 光照最远距离
    }

    declare object3d: THREE.PointLight & Object3D;

    // 颜色缓动对象
    Tcolor = new TransitionColor(this.props.color || "#fff");

    // 其他数值型参数
    lightParams = new Transition(); // 其他参数

    componentBeforeRender(renderer: THREE.WebGLRenderer, scene: THREE.Scene, camera: THREE.Camera) {
        this.setLightParam();
    }

    setLightParam() {
        let keepRun = false;

        // 光照颜色
        if (!this.Tcolor.isDone()) {
            const { r, g, b } = this.Tcolor.getJson();
            this.object3d.color.setRGB(r, g, b);
            keepRun = true;
        }

        // 光照强度
        if (!this.lightParams.isDone()) {
            const { intensity, decay, distance } = this.lightParams.getJson();
            Object.assign(this.object3d, {
                intensity,
                decay,
                distance
            });
            keepRun = true;
        }

        return keepRun;
    }

    // 设置灯光过渡参数
    setLightTansParam() {
        const {
            bezier,
            duration = 500,
            color = "#fff",
            intensity = 1,
            castShadow = false,
            decay = 0,
            distance = 100
        } = this.props;

        this.Tcolor.setBezier(bezier).setDuration(duration).set(color);

        this.lightParams.setBezier(bezier).setDuration(duration).set({ intensity, decay, distance });

        this.object3d.castShadow = castShadow;
    }

    componentDidMount() {

        // 减少双面物体的自阴影
        this.object3d.shadow.bias = -0.05;

        this.setLightTansParam();
        this.setLightParam();
    }

    componentDidUpdate() {
        this.setLightTansParam();
    }

    render() {

        const { dev } = this.context;
        const { color = "#fff" } = this.props;

        if (!dev) {
            return null;
        }

        return (
            <Mesh
                duration={0}
                geometry={ball}
                side={THREE.FrontSide}
                material="MeshBasicMaterial"
                wireframe={true}
                clipping={false}
                color={color}
                cursor="pointer"
                title={
                    <div
                        style={{
                            position: "relative",
                            backgroundColor: "rgba(0, 0, 0, 0.8)",
                            borderRadius: 4,
                            padding: "4px 8px",
                            color: "#fff"
                        }}
                    >
                        <span>{"点光源"}<br /></span>
                        <span>{"<PointLight/>"}<br /></span>
                    </div>
                }
            />
        );
    }
}